﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Ext.Net;
using Ext.Net.MVC;
using WebEntities;
using WebEntities.Models.WebInfo;
using com.subb.WebInfo;

namespace WebUI.Areas.WebAdmin.Controllers
{
    [Authorize]
    public class HonorTypeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult GetAllHonorTypeByPage(StoreRequestParameters parms)
        {
            WI_HonorTypeDAL hdal = new WI_HonorTypeDAL();
            int ttlCount;
            List<WI_HonorType> wihorlist = hdal.GetAllHonorType(parms.Start, parms.Limit, out ttlCount);
            List<HonorType> horList = new List<HonorType>();
            foreach (var wihorType in wihorlist)
            {
                HonorType honorType = HonorType.AsHonorType(wihorType);
                horList.Add(honorType);
            }
            return this.Store(horList, ttlCount);
        }

        public ActionResult GetAllHonorType()
        {
            WI_HonorTypeDAL hdal = new WI_HonorTypeDAL();
            List<WI_HonorType> wihorlist = hdal.GetAllHonorType();
            List<HonorType> horList = new List<HonorType>();
            foreach (var wihorType in wihorlist)
            {
                HonorType honorType = HonorType.AsHonorType(wihorType);
                horList.Add(honorType);
            }
            return this.Store(horList);
        }

        [HttpPost]
        public ActionResult DeleteHonorType(List<HonorType> delHonorType)
        {
            try
            {
                WI_HonorTypeDAL hdal = new WI_HonorTypeDAL();
                foreach (var honorType in delHonorType)
                {
                    hdal.RemoveHonorType(honorType.HonorTypeID);
                }
                return this.Direct(new { Success = true, Msg = "删除成功！" });
            }
            catch (Exception ex)
            {
                return this.Direct(new { Success = false, Msg = ex.InnerException != null ? ex.InnerException.Message : ex.Message });
            }
        }

        public ActionResult NewHonorTypeWindow()
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "新建荣誉类别",
                Width = 300,
                Height = 200,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("CreateHonorType"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Render();
            return this.Direct();
        }

        public ActionResult CreateHonorType()
        {
            return View();
        }

        [HttpPost]
        public ActionResult CreateHonorType(string HonorTypeName)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_HonorTypeDAL hdal = new WI_HonorTypeDAL();
                hdal.CreateHonorType(HonorTypeName);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "类别创建成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }

        public ActionResult EditHonorTypeWindow(int typeId)
        {
            Window wnd = new Window()
            {
                ID = "Window1",
                Title = "编辑类别",
                Width = 300,
                Height = 200,
                Modal = true,
                Maximizable = true,
                Icon = Ext.Net.Icon.TextListBullets,
                Loader = new ComponentLoader
                {
                    Url = Url.Action("EditHonorType"),
                    Mode = LoadMode.Frame,
                    LoadMask =
                    {
                        ShowMask = true,
                        Msg = "加载中，请稍候..."
                    }
                }
            };
            wnd.Loader.Params.Add(new Parameter("typeId", typeId.ToString()));
            wnd.Render();
            return this.Direct();
        }

        public ActionResult EditHonorType(int typeId)
        {
            WI_HonorTypeDAL hdal = new WI_HonorTypeDAL();
            WI_HonorType wihonType = hdal.GetHonorType(typeId);
            HonorType htype = HonorType.AsHonorType(wihonType);
            return View(htype);
        }

        [HttpPost]
        public ActionResult EditHonorType(HonorType htype)
        {
            FormPanelResult response = new FormPanelResult();
            try
            {
                WI_HonorTypeDAL hdal = new WI_HonorTypeDAL();
                hdal.UpdateHonorType(htype.HonorTypeID, htype.HonorTypeName);
                response.Success = true;
                response.ExtraParams.Add(new Parameter("Msg", "类别更新成功！"));
                return response;
            }
            catch (Exception ex)
            {
                response.Success = false;
                response.ExtraParams.Add(new Parameter("Msg", ex.InnerException != null ? ex.InnerException.Message : ex.Message));
                return response;
            }
        }
    }
}
